var msgListKey = "";
Page({
  data:{
    // 历史记录
    sercherStorage: [],
    // 新增词条字段
    NullStorage:"none",
    // 显示历史记录
    iss:"none",
    // 热门搜索
    movies: [],
    // 清除输入框的叉叉
    cl1:'none',
    // 输入框的内容
    searchinput:'',
    // 历史记录样式边框变圆
    removebsty: '',
    // 是否执行
    isture:true,
  },
  // 删除单一历史记录叉叉
  removefa:function(even){
    var that = this;
    var id = even.target.dataset.id;
    var searchLogDatas = this.data.sercherStorage;
    for (var index in searchLogDatas) {
      if (searchLogDatas[index]['id'] == id) {
        delete searchLogDatas[index];
      }
    }
    // searchLogDatas = that.arr_del(searchLogDatas,id)
    if (searchLogDatas.length<1){
      searchLogDatas = ""
    }
    console.log(searchLogDatas)
    wx.setStorageSync('searchLog', searchLogDatas);
    that.setData({
      searchinput:'',
      isture:false,
    })
    return;
  },
  // 监听输入框事件
  changinput:function(e){
    var sercherStorage = this.data.sercherStorage;
    if (e.detail.value) {
      if (sercherStorage.length>1){
        var iss = "block";
      }else{
        var iss = "none";
      }
      this.setData({
        cl1: "block",
        iss: iss,
        removebsty:'removebsty',
        NullStorage: "none",
      })
    }
  },
  // 数组删除
   arr_del:function(arr,node){
      for(var i = 0; i<arr.length; i++){
        if (arr[i] == node) {
          arr.splice(i, 1);
          return arr;
        }
    }
      return arr;
  },
  // 历史记录存放和监听输入框事件
  BeginSearch: function (e) {
    console.log(e)
    this.setData({
      searchinput: '',
      cl1: 'none',
      iss: "none",
      removebsty:'',
      NullStorage: "none",
    })
  },
  tapSercherStorage:function(e){
    console.log(e);
    var isture = this.data.isture;
    if (isture){
      var data = e.currentTarget.dataset;
      this.setData({
        searchinput: '' + data.name,
      })
    }else{
      return;
    }
    
  },
  blurInputEvent:function(e){
    this.setData({
      iss: "none",
      removebsty: '',
      NullStorage: "none",
    })
  },
  // 保存本地缓存
  setsearchData:function(name){
    var that = this;
    if(name){
      var searchLogData = that.data.sercherStorage;
      var id = (searchLogData.length);
      if(id>0){
        for (var index in searchLogData) {
          if (searchLogData[index]['name'] == name) {
            delete searchLogData[index];
            var data = { id: id, name: '' + name }
          } else {
            var data = { id: id, name: '' + name }
          }
        }
      }else{
        var data = { id: id, name: '' + name }
      }
      searchLogData.push(data);
      wx.setStorageSync('searchLog', searchLogData);
    }
  },
  // 搜索
  iscture: function (e) {
    var name = e.detail.value
    var that = this;
    if(!name){
      that.setData({
        NullStorage: "none",
      })
      return
    }
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/search_like_name',
      method: 'GET',
      data: {
        name: "" + name
      },
      success: function (res) {
        if (!res.data){
          that.setData({
            NullStorage:"block",
          })
        }else{
          // 当输入成功之后跳转打开缓存
          that.setsearchData(name);
          wx.navigateTo({
            url: '../Search_path/Search_path?name=' + name
          })
        }
      },
      fail:function(){
        that.setData({
          NullStorage: "none",
        })
      }
    })
  },
  // 热门搜索跳转
  hot_search: function (e) {
    var e = e.target.dataset;
    console.log(e)
    if (!e.its_id) {
      var urls = '../Allot/Allot?id=' + e.bangdan_id + "&tid=" + e.teacase_id + '&hid=' + e.teaarea_id
    } else {
      if (e.bangdan_id == 1) {
        var urls = getApp().Craftsman +'?sid=' + e.its_id + '&id=' + e.bangdan_id + '&tid=' + e.teacase_id + '&hid=' + e.teaarea_id + "&ins=" + e.ins + '&biaozhi=1';
      } else if (e.bangdan_id == 2) {
        var urls = getApp().brand +'?sid=' + e.its_id + '&id=' + e.bangdan_id + '&tid=' + e.teacase_id + '&hid=' + e.teaarea_id + "&ins=" + e.ins + '&biaozhi=1';
      } else {
        var urls = getApp().Treasures +'?sid=' + e.its_id + '&id=' + e.bangdan_id + '&tid=' + e.teacase_id + '&hid=' + e.teaarea_id + "&ins=" + e.ins + '&biaozhi=1';
      }
    }
    console.log(urls);
    wx.navigateTo({
      url: urls
    })
  },
  bindViewTap: function () {
    wx.navigateTo({
      url: '../index/index'
    })
  },
  locationAdd:function(){
    wx.navigateTo({
      url: '../Add/Add'
    })
  },
  locationlw: function () {
    wx.navigateTo({
      url: '../Contact/Contact'
    })
  },
  locationdesc: function () {
    wx.navigateTo({
      url: '../Description/Description'
    })
  },

  //清除缓存历史
  clearSearchStorage: function () {
    wx.removeStorageSync('searchData')
    this.setData({
      sercherStorage: [],
      StorageFlag: false,
    })
  },
  //打开历史记录列表
  openLocationsercher: function () {
    this.setData({
      sercherStorage: wx.getStorageSync('searchData') || [],
      StorageFlag: true,
      listFlag: true,
    })
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    });
    // 打开缓存
    try {
      var info = wx.getStorageSync('searchLog')
      console.log(info);
      if (info) {
        that.setData({
          sercherStorage: info
        });
      } 
    } catch (e) {
      // Do something when catch error
    }
    
    // 热门搜索
    wx.request({
      url: getApp().AppUrl + '/index.php/api/api/hot_search_ct',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var botton = res.data;
        that.setData({
          movies: botton
        })
      }
    })


  },

})